package com.cats.template.sys.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.cats.template.sys.pojo.dto.UserDto;
import com.cats.template.sys.utils.UserInfoUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author duxiaobo
 * @date 2021/10/126:02 下午
 */
@Slf4j
@Component
public class MetaHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        UserDto currentUser = UserInfoUtils.getCurrentUser();
        this.setFieldValByName("createdAt", LocalDateTime.now(), metaObject);
        this.setFieldValByName("createdBy", currentUser.getId(), metaObject);
        this.setFieldValByName("updatedAt", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updatedBy", currentUser.getId(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        UserDto currentUser = UserInfoUtils.getCurrentUser();
        this.setFieldValByName("updatedAt", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updatedBy", currentUser.getId(), metaObject);
    }
}
